package leeon.bbsbrower.views;

import leeon.bbsbrower.actions.Config;
import leeon.bbsbrower.util.BrowerImages;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
	
	//默认大字体
	public static Font defaultFont = new Font(null, new FontData("", 20, SWT.NORMAL));;
	
	//列表中使用的字体
	public static Font listFont = new Font(null, new FontData("", 14, SWT.NORMAL));;
	
	public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {		
		super(configurer);
		configurer.setShellStyle(SWT.CLOSE|SWT.TITLE);
	}

	public ActionBarAdvisor createActionBarAdvisor(
			IActionBarConfigurer configurer) {
		return new ApplicationActionBarAdvisor(configurer);
	}

	public void preWindowOpen() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		configurer.setInitialSize(new Point(Config.SCREEN_SIZE_W, Config.SCREEN_SIZE_H));
		configurer.setShowCoolBar(false);
		configurer.setTitle("BBS Brower");		
	}
	
	
	public void postWindowOpen() {		
		IWorkbenchPage page = getWindowConfigurer().getWindow().getActivePage();
		//隐藏不用的视图
		page.hideView(page.findView(MainView.ID));
		page.hideView(page.findView(FriendView.ID));
		page.hideView(page.findView(MailView.ID));
		page.hideView(page.findView(BlockView.ID));
		page.hideView(page.findView(AllBoardView.ID));
		page.hideView(page.findView(MyBoardView.ID));
		page.hideView(page.findView(BoardListView.ID));
		page.hideView(page.findView(DocView.ID));
		page.hideView(page.findView(ConView.ID));
		page.hideView(page.findView(MailListView.ID));
		page.hideView(page.findView(MailConView.ID));
		page.hideView(page.findView(UserListView.ID));
		page.hideView(page.findView(UserConView.ID));
		page.hideView(page.findView(AllMsgView.ID));
		page.hideView(page.findView(SendMsgView.ID));
		page.hideView(page.findView(FindUserView.ID));
		page.hideView(page.findView(SendMailView.ID));
		page.hideView(page.findView(SendDocView.ID));
	}
	
	public void createWindowContents(final Shell shell) {				
		super.createWindowContents(shell);
		
		//获取system tray,并添加一个item
		Tray tray = shell.getDisplay().getSystemTray();
		TrayItem item = new TrayItem(tray, SWT.NONE);
		item.setImage(BrowerImages.getImage(BrowerImages.ICONS_OBJ_SYSTRAY));		
		item.setToolTipText("BBS Brower");
		
		//设定shell的图标
		shell.setImage(BrowerImages.getImage(BrowerImages.ICONS_OBJ_SYSTRAY));
		
		//选中事件,显示窗口
		item.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				ApplicationWorkbenchWindowAdvisor.this.
					getWindowConfigurer().
					getWindow().
					getShell().
					setVisible(true);				
			}

			public void widgetDefaultSelected(SelectionEvent e) {}			
		});
		hookContextMenu(item);
	}
	
	
	//将菜单加入到system tray右键中
	private void hookContextMenu(TrayItem item) {
		Shell shell = getWindowConfigurer().getWindow().getShell();
		final Action showWindow = new Action() {
			public void run() {
				Shell shell = ApplicationWorkbenchWindowAdvisor.this.
					getWindowConfigurer().
					getWindow().
					getShell();
				shell.setVisible(!shell.getVisible());
			}
		};
		showWindow.setText("显示窗口");
		
		MenuManager menuMgr = new MenuManager("#TrayPopupMenu");
		
		final Menu menu = menuMgr.createContextMenu(shell);
		menuMgr.add(showWindow);
		menuMgr.add(new Separator());
		menuMgr.add(ApplicationActionBarAdvisor.exitAction);
		
	    	    
	    item.addListener(SWT.MenuDetect, new Listener() {
	        public void handleEvent(Event event) {
	        	menu.setVisible(true);
	        	Shell shell = ApplicationWorkbenchWindowAdvisor.this.
					getWindowConfigurer().
					getWindow().
					getShell();
	        	if (shell.getVisible()) 
	        		showWindow.setText("隐藏窗口");
	        	else
	        		showWindow.setText("显示窗口");
	        }
	    });
	}

	// 屏蔽shell关闭事件,将其设定成invisible
	public boolean preWindowShellClose() {
		this.getWindowConfigurer().getWindow().getShell().setVisible(false);
		return false;
	}
	
	// windows关闭后
	public void postWindowClose() {
		super.postWindowClose();
	}
}
